<template>
  <div class="platform">
    <!-- header -->
    <van-nav-bar
      title="平台新增"
      left-text="返回"
      class="platform-nav"
      left-arrow
      ref="header"
      @click-left="onClickLeft"
    />
    <!-- content -->
    <van-cell-group class="platform-main">
      <van-field v-model="name" placeholder="请输入平台名称" />
      <van-field v-model="account" placeholder="请输入平台账号" />
      <van-field v-model="password" placeholder="请输入平台密码" type="password" />
      <van-field v-model="url" placeholder="请输入平台网址" />
      <van-field v-model="downUrl" placeholder="请输入APP下载地址" />
      <van-button type="danger" block @click="onAddPlatform">确认新增</van-button>
    </van-cell-group>
  </div>
</template>

<script>
export default {
  name: "Platform",
  data() {
    return {
      name: "",
      account: "",
      password: "",
      url: "",
      downUrl: ""
    };
  },
  mounted() {
    const _this = this;
    this.$nextTick(() => {
      var statusBar = window.api.require("statusBar");
      //异步返回结果：
      statusBar.getStatusBarHeight(function(ret) {
        _this.$refs.header.$el.style.height = ret.statusHeight + 44 + "px";
        _this.$refs.header.$el.style.paddingTop = ret.statusHeight + "px";
        _this.$refs.header.$el.getElementsByClassName(
          "van-nav-bar__left"
        )[0].style.top = ret.statusHeight + "px";
      });
    });
  },
  methods: {
    // 返回
    onClickLeft() {
      window.api.closeWin();
    },
    // 新增平台
    onAddPlatform() {
      if (this.name === "") {
        this.$toast("平台名称不能为空");
      } else if (this.account === "") {
        this.$toast("平台账户不能为空");
      } else if (this.password === "") {
        this.$toast("平台账户密码不能为空");
      } else if (this.url === "" && this.downUrl === "") {
        this.$toast("平台网址或下载地址不能为空");
      } else {
        window.api.showProgress({
          title: "操作中...",
          text: "先喝杯茶...",
          modal: false
        });
        this.$axios
          .post(
            "http://118.25.10.223:8095/t/platform/add_platform",
            {
              name: this.name,
              account: this.account,
              password: this.password,
              url: this.url,
              downUrl: this.downUrl
            },
            {
              headers: {
                "content-type": "application/json",
                token: this.$storage.get("token")
              }
            }
          )
          .then(res => {
            window.api.hideProgress();
            if (res.code === 0) {
              this.$toast("新增成功");
            } else {
              this.$toast(res.msg);
            }
          }).catch(res => {
            window.api.hideProgress();
            this.$toast(res.message);
          });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.platform {
  width: 100%;
  @include flex(
    $direction: column,
    $justifyContent: flex-start,
    $flexWrap: nowrap
  );
  .platform-main {
    width: 100%;
    .van-button {
      width: 343px;
      margin-left: 16px;
      margin-top: 30px;
      padding: 0 15px;
    }
  }
}
</style>
